Page({
  data: {
    schema:[
    ],
    formData:{
    },
    title: '',
    result: '',
    remark: '',
    apiName: '',
    liveNoticeInfo:null, //直播预告信息
    liveInfo: null,      // 直播信息
    finderUserName: '',  // 视频号ID
    startTimeStr: '',    // 开始时间字符串（展示用）
    endTimeStr: '',      // 结束时间字符串（展示用）
    startTime: '',       // 开始时间戳
    endTime: '',         // 结束时间戳
    currentDate: '',     // 当前日期（用于限制选择范围）
  },
  // 视频号ID输入变化
  onFinderUserNameChange(e) {
    this.setData({
      finderUserName: e.detail.value
    });
  },

  // 开始时间选择变化
  onStartTimeChange(e) {
    this.setData({
      startTimeStr: e.detail.value,
      startTime: this.dateToTimestamp(e.detail.value)
    });
  },

  // 结束时间选择变化
  onEndTimeChange(e) {
    this.setData({
      endTimeStr: e.detail.value,
      endTime: this.dateToTimestamp(e.detail.value)
    });
  },
  // 日期字符串转时间戳（秒）
  dateToTimestamp(dateStr) {
    if (!dateStr) return '';
    return Math.floor(new Date(dateStr).getTime() / 1000);
  },
  //获取视频号直播信息
  getChannelsLiveInfo(e){
    let that = this
    // 构建参数
    const params = {};
    // 基本校验
    if (!that.data.finderUserName) {
      wx.showToast({
        title: '请输入视频号ID',
        icon: 'none'
      })
      return;
    }
    params.finderUserName = that.data.finderUserName
    // 只有选择了才添加时间参数
    if (that.data.startTime) params.startTime = that.data.startTime;
    if (that.data.endTime) params.endTime = that.data.endTime;
    wx.getChannelsLiveInfo({
      ...params,
      success: (res) => {
        that.setData({
          liveInfo:res,
          result: res,
          apiName: e.currentTarget.dataset.apiname 
        })
      },
      fail(err) {
        that.setData({
          result: err,
          apiName: e.currentTarget.dataset.apiname 
        })
      }
    })
  },
  //获取视频号直播预告信息
  getChannelsLiveNoticeInfo(e) {
    let that = this
    if (!that.data.finderUserName) {
      wx.showToast({
        title: '请输入视频号ID',
        icon: 'none'
      })
      return;
    }
    wx.getChannelsLiveNoticeInfo({
      finderUserName:that.data.finderUserName,
      success: (res) => {
        that.setData({
          liveNoticeInfo:res,
          result: res,
          apiName: e.currentTarget.dataset.apiname 
        })
      },
      fail(err) {
        that.setData({
          result: err,
          apiName: e.currentTarget.dataset.apiname 
        })
      }
    })
  },
  //预约视频号直播
  reserveChannelsLive(e){
    let that = this
    if (!that.data.finderUserName) {
      wx.showToast({
        title: '请输入视频号ID',
        icon: 'none'
      })
      return;
    }
    if(!that.liveNoticeInfo){
      wx.showToast({
        title: '还未获取到预告 id',
        icon: 'none'
      })
      return
    }
    wx.reserveChannelsLive({
      noticeId:that.liveNoticeInfo.noticeId,
      success: (res) => {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname 
        })
      },
      fail(err) {
        that.setData({
          result: err,
          apiName: e.currentTarget.dataset.apiname 
        })
      }
    })
  },
  //打开视频号主页
  openChannelsUserProfile(e){
    let that = this
    if (!that.data.finderUserName) {
      wx.showToast({
        title: '请输入视频号ID',
        icon: 'none'
      })
      return;
    }
    wx.reserveChannelsLive({
      finderUserName:that.data.finderUserName,
      success: (res) => {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname 
        })
      },
      fail(err) {
        that.setData({
          result: err,
          apiName: e.currentTarget.dataset.apiname 
        })
      }
    })
  },
  //打开视频号直播
  openChannelsLive(e){
    let that = this
    if (!that.data.finderUserName) {
      wx.showToast({
        title: '请输入视频号ID',
        icon: 'none'
      })
      return;
    }
    if(!that.liveInfo){
      wx.showToast({
        title: '还未获取到视频号直播信息',
        icon: 'none'
      })
      return
    }
    wx.reserveChannelsLive({
      finderUserName:that.data.finderUserName,
      feedId:that.data.liveInfo.feedId,
      nonceId:that.data.liveInfo.nonceId,
      success: (res) => {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname 
        })
      },
      fail(err) {
        that.setData({
          result: err,
          apiName: e.currentTarget.dataset.apiname 
        })
      }
    })
  },
  //打开视频号视频
  openChannelsActivity(e){
    let that = this
    if (!that.data.finderUserName) {
      wx.showToast({
        title: '请输入视频号ID',
        icon: 'none'
      })
      return;
    }
    if(!that.liveInfo){
      wx.showToast({
        title: '还未获取到视频号直播信息',
        icon: 'none'
      })
      return
    }
    wx.openChannelsActivity({
      finderUserName:that.data.finderUserName,
      feedId:that.data.liveInfo.feedId,
      nonceId:that.data.liveInfo.nonceId,
      success: (res) => {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname 
        })
      },
      fail(err) {
        that.setData({
          result: err,
          apiName: e.currentTarget.dataset.apiname 
        })
      }
    })
  },
  //获取视频号直播卡片/视频卡片的分享来源
  getChannelsShareKey(e){
    let that = this
    wx.getChannelsShareKey({
      success: (res) => {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname 
        })
      },
      fail(err) {
        that.setData({
          result: err,
          apiName: e.currentTarget.dataset.apiname 
        })
      }
    })
  },
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: options.title,
    })
    
    this.setData({
      title: options.title,
      zh:options.zh,
      remark: options.remark,
      webUrl: options.webUrl
    })
    // 设置当前日期为结束时间的最大可选值
    const now = new Date();
    const year = now.getFullYear();
    const month = (now.getMonth() + 1).toString().padStart(2, '0');
    const day = now.getDate().toString().padStart(2, '0');
    this.setData({
      currentDate: `${year}-${month}-${day}`
    });
  }
})
